import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:syria_sports/data/controllers/subscriptions_controller.dart';

import '../../../../core/utils/show_toast.dart';
import '../../../../data/models/auth/user.dart';
import '../../subscriptions_page/models/subscription.dart';

part 'user_subscriptions_state.dart';

class UserAttendancesListCubit extends Cubit<UserSubscriptionsState> {
  UserAttendancesListCubit() : super(UserSubscriptionsInitial());

  static UserAttendancesListCubit get(context) => BlocProvider.of(context);

  List<Subscription> subscriptions = [];
  late User user;

  void init({ required User user}) {
    this.user = user;

    getAllSubscriptions();
  }

  Future<void> getAllSubscriptions() async {
    emit(UserSubscriptionsLoading());
    await SubscriptionsController.getUserSubscriptions( user:user).catchError((e) {
      emit(UserSubscriptionsError());
      return null;
    }).then((value) {
      if (value != null && value.state) {
        subscriptions = List<Subscription>.generate(value.data.length, (index) =>
            Subscription.fromJson(value.data[index]));
        emit(UserSubscriptionsDone());
      } else if (value != null) {
        showMessage(value.message);
        emit(UserSubscriptionsError());
      } else {
        if (kDebugMode) {
          print('value is null');
        }
        emit(UserSubscriptionsError());
      }
    });
  }

}
